import java.util.Stack;

class Solution {
    public boolean isValid(String s) {
        Stack<Character>stack=new Stack<>();
        for(int i=0;i<s.length();i++){
            char ch=s.charAt(i);
            //左括号入栈
            if(ch=='('||ch=='['||ch=='{')
            {
                stack.push(ch);
            }else{
                //右括号出栈

                //栈为空说明右括号多
                if(stack.empty()){
                    return false;
                }
                char chl=stack.peek();
                if(chl=='('&&ch==')'||chl=='['&&ch==']'||chl=='{'&&ch=='}')
                {
                    stack.pop();
                }else{
                    //括号不匹配
                    return false;
                }
            }
        }
        //栈不为空说明左括号多
        return stack.empty();
    }
}